﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Xml;

namespace CFScript.Tasks
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Echo:Task
    {
        public Echo()
        {
            this.name = "<echo>";
            this.parameterNames = new string[] {"message"};
            this.parameterExpandable = new bool[] { true };
            this.parameterRequired = new bool[] {false};
            this.parameters = new Dictionary<string, Parameter>();
            this.hasInnerText = true;

            Debug.Assert(this.parameterNames.Length == this.parameterRequired.Length &&
                         this.parameterRequired.Length == this.parameterExpandable.Length);
        }

        public override InstructionResult Execute(CFScript context)
        {
            InstructionResult result = new InstructionResult();
            result.ExecutedInstruction = this;

            string messageToEcho = "";

            try
            {
                if (!string.IsNullOrEmpty(this.innerText))
                {
                    messageToEcho = context.ExpandValue(this.innerText);
                }
                else if (!string.IsNullOrEmpty(this.parameters["message"].GetValue(context)))
                {
                    //Console.WriteLine(this.parameters["message"].GetValue(context));
                    messageToEcho = this.parameters["message"].GetValue(context);
                }

                if (context.OutputStream != null)
                {
                    context.OutputStream.WriteLine(messageToEcho);
                    result.Code = ResultCode.OK;
                    switch (context.Options.VerboseLevel)
                    {
                        case VerboseMode.MINIMUM:
                            result.Message = string.Format("Message sent to OutputStream");
                            break;
                        case VerboseMode.ALL:
                            result.Message = string.Format("Message {0} sent to OutputStream", messageToEcho);
                            break;
                    }
                }
                else
                {
                    result.Code = ResultCode.WARN;
                    switch (context.Options.VerboseLevel)
                    {
                        case VerboseMode.MINIMUM:
                        case VerboseMode.ALL:
                            result.Message = string.Format("OutputStream not present");
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERROR;
                result.Message = ex.Message;
            }

            result.BreakExecution = result.Code == ResultCode.ERROR && this.options.OnError == OnErrorAction.BREAK;

            return result;
        }
    }
}
